ONNX
格式。此格式可以在不同的平台上,高效运行,包括 PHP 应用程序。
Transformers PHP 是一个开源项目。您可以在 GitHub 存储库中找到更多信息和源代码。
ONNX Runtime PHP
Transformers PHP 是一个开源项目。您可以在 这里的 GitHub 存储库 中找到更多信息和源代码。
Transformers PHP 的核心在于它与 ONNX Runtime 的集成,ONNX Runtime 是一种高性能 AI 引擎,旨在高效执行深度学习模型。利用外部函数接口 (FFI) 机制,Transformers PHP 可以无缝连接 ONNX Runtime,从而能够在 PHP 环境中以闪电般的速度执行 Transformer 模型。
那么,ONNX Runtime 到底是什么?ONNX - 开放神经网络交换 的核心是一种表示深度学习模型的开放格式,可促进各种框架之间的互操作性。由 Microsoft 开发的 ONNX Runtime 是一款专为 ONNX 模型构建的跨平台高性能引擎。它为跨不同硬件平台(包括 CPU、GPU 和专用加速器)高效执行神经网络模型提供了强大的支持。
composer require codewithkyrian/transformers
在执行命令期间,系统会询问您是否启用并运行 ankane/onnxruntime 包的 composer 插件,以下载 PHP 的 ONNXRuntime 二进制文件。选择 y:
Do you trust "codewithkyrian/onnxruntime-downloader-plugin" to execute code and wish to enable it now? (writes "allow-plugins" to composer.json) [y,n,d,?]
composer 将下载并安装 vendor/
文件夹中的所有依赖项,并自动下载 ONNX 运行时,因此您只需运行 composer require codewithkyrian/transformers 最后的 ONNX Runtime Downloader 插件非常简单,它借助 ONNX Runtime PHP 包自动触发 ONNX Runtime 的下载。
<?php // 001 requiring the autoload file from vendor require './vendor/autoload.php'; // 002 importing the Transformers class use Codewithkyrian\Transformers\Transformers; // 003 importing the pipeline function use function Codewithkyrian\Transformers\Pipelines\pipeline; // 004 initializing the Transformers class setting the cache directory for models Transformers::setup()->setCacheDir('./models')->apply(); // 005 initializing a pipeline for sentiment-analysis $pipe = pipeline('sentiment-analysis'); // 006 setting the list of sentences to analyze $feedbacks = [ 'The quality of tools in the PHP ecosystem has greatly improved in recent years', "Some developers don't like PHP as a programming language", 'I appreciate Laravel as a framework', 'Laravel is a framework that improves my productivity', 'Using an outdated version of Laravel is not a good practice', 'I love Laravel', ]; echo PHP_EOL.'⭐⭐⭐ SENTIMENT ANALYSIS ⭐⭐⭐'.PHP_EOL.PHP_EOL; // 007 looping thrgouh the sentences foreach ($feedbacks as $input) { // 008 calling the pipeline function $out = $pipe($input); // 009 using the output of the pipeline function $icon = $out['label'] === 'POSITIVE' ? ($out['score'] > 0.9997 ? '???' : '?? ') : '? '; echo $icon.' '.$input.PHP_EOL; } echo PHP_EOL;